#include "socketmanager.h"

Q_GLOBAL_STATIC(socketmanager, sktmanager)
socketmanager::socketmanager()
{
    socket_init();
}

socketmanager *socketmanager::GetInstance()
{
    return  sktmanager;
}

void socketmanager::socket_init()
{
    QByteArray ba;
    this->udpSocket = new QUdpSocket;
    this->tcpSocket = new QTcpSocket;

    //获取配置文件端口号
    QString config_path = "/etc/kylin-activation/activation_conf.ini";
    QSettings settings(config_path, QSettings::IniFormat);
    ba = settings.value("KMS_Activation/UDP_Port").toByteArray();
    this->udpPort = QString(ba);
    ba = settings.value("KMS_Activation/TCP_Port").toByteArray();
    this->tcpPort = QString(ba);

    if (this->udpPort=="default") {//default: 17070
        this->udp_port = 17070;
        qDebug()<<"Udp defaule port bind to 17070 .";
    } else if(this->udpPort=="") {
        this->udp_port = -1;
        qDebug()<<"Udp randomly assigns port numbers .";
    } else {
        if (this->udpPort.toInt() > 0 && this->udpPort.toInt() < 65536) {
            this->udp_port = this->udpPort.toInt();
            qDebug()<<"Udp port bind:"<<this->udp_port;
        } else {
            this->udp_port = -1;
            qDebug()<<"KMS_Activation/UDP_Port Format error, Udp randomly assigns port numbers.";
        }
    }

    if (this->tcpPort=="default") {//default: 17071
        this->tcp_port = 17071;
        qDebug()<<"Tcp defaule port bind to 17071 .";
    } else if(this->tcpPort=="") {
        this->tcp_port = -1;
        qDebug()<<"Tcp randomly assigns port numbers .";
    } else {
        if (this->tcpPort.toInt() > 0 && this->tcpPort.toInt() < 65536) {
            this->tcp_port = this->tcpPort.toInt();
            qDebug()<<"Tcp port bind:"<<this->tcp_port;
        } else {
            this->tcp_port = -1;
            qDebug()<<"KMS_Activation/TCP_Port Format error, Tcp randomly assigns port numbers .";
        }
    }
}

void socketmanager::udpSocket_port_bind(int port)
{
    bool ret = 0;
    if (port > 0 && port < 65536) {
        ret = this->udpSocket->bind(QHostAddress::AnyIPv4,port,QAbstractSocket::ReuseAddressHint);
        this->udp_port = port;
        qDebug()<<"Bind UDP socket port: "<<port<<", result: "<<ret;
    }
}

void socketmanager::tcpSocket_port_bind(int port)
{
    bool ret = 0;
    if (port > 0 && port < 65536) {
        ret = this->tcpSocket->bind(QHostAddress::Any,port,QAbstractSocket::ReuseAddressHint);
        this->tcp_port = port;
        qDebug()<<"Bind TCP socket port: "<<port<<", result: "<<ret;
    }
}

QUdpSocket* socketmanager::get_udpSocket()
{
    //reset udp port
    if (this->udp_port == -1)
        return this->udpSocket;
    if (this->udp_port != 0 && this->udpSocket->localPort() != this->udp_port)
        this->udpSocket_port_bind(this->udp_port);

    return this->udpSocket;
}

QTcpSocket* socketmanager::get_tcpSocket()
{
    //reset tcp port
    if (this->tcp_port == -1)
        return this->tcpSocket;
    if (this->tcp_port != 0 && this->tcpSocket->localPort() != this->tcp_port)
        this->tcpSocket_port_bind(this->tcp_port);
    return this->tcpSocket;
}
